package de.szut.ita13.sommerfeld.loganalyser;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.Frame;
import java.awt.Point;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;

import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.TableColumn;

public class UserSearchResultWindow extends Frame {
	
	private static final long serialVersionUID = 1L;
	
	private String search;
	private User user;

	public UserSearchResultWindow(String search, User user) {
		this.user = user;
		this.search = search;
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				if(we.getSource() == UserSearchResultWindow.this) {
					we.getWindow().setVisible(false);
				}
			}
		});
		
		setSize(new Dimension(1200,600));
		setResizable(false);
		
		user = (User)LogHandler.allUser.get(search);
		ArrayList logs = user.getLogs();
		setTitle(logs.size() + " Logs for User: " + search);
		
		JTable table = new JTable(getData(logs.size(), logs), Log.logColumnNames);
		table.setCellSelectionEnabled(false);
		
		TableColumn column = null;
		for (int i = 0; i < Log.logColumnNames.length; i++) {
		    column = table.getColumnModel().getColumn(i);
		    column.setPreferredWidth(Log.logColumnsPrefferedWidth[i]);
		}
		
		JScrollPane scrollPane = new JScrollPane(table);
		table.setFillsViewportHeight(true);
		add(scrollPane);
		table = new JTable();
	}	
	
	public UserSearchResultWindow(String search, String error) {
		setTitle(search);
		setLayout(null);
		setSize(new Dimension(1200,600));
		setResizable(false);
		
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent we) {
				if(we.getSource() == UserSearchResultWindow.this) {
					we.getWindow().setVisible(false);
				}
			}
		});
		
		JLabel label = new JLabel(error);
		label.setFont(new Font("Arial",Font.BOLD,30));
		label.setSize(new Dimension(1000,50));
		label.setLocation(new Point(this.getWidth() / 2 - label.getWidth() / 2, this.getHeight() / 2  - label.getHeight() / 2));
		label.setVisible(true);
		add(label);
	}
	
	
	private Object[][] getData(int logcount, ArrayList logs) {
		Object[][] data = new Object[logcount][Log.logColumnNames.length];	
		for(int i = 0; i < logs.size(); i++) {
			Log log = (Log)logs.get(i);
			String[] dataRow = {log.getIpadress(),log.getHyphen(),log.getUserid(),log.getDateString(),log.getRequestCmd(),log.getResource(),log.getProtocol(),String.valueOf(log.getStatus()),String.valueOf(log.getSize())};
			for(int j = 0; j < dataRow.length; j++) {
				data[i][j] = dataRow[j];
			}
		}
		return data;
	}

	public String getSearch() {
		return search;
	}

	public void setSearch(String search) {
		this.search = search;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	
}
